package com.wing.order.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.config.BaseDataInit;
import com.wing.order.model.response.OrderDeliveryVO;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.OrderDeliverySign;
import com.wing.order.model.response.OrderDeliverySignVO;
import com.wing.order.service.OrderDeliverySignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.order.model.request.OrderDeliverySignForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-签收记录管理", tags = "签收记录管理")
@RestController("appOrderDeliverySignController")
@RequestMapping("order/app/orderDeliverySign")
public class OrderDeliverySignController {

    @Autowired
    private OrderDeliverySignService orderDeliverySignService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "已签收列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderDeliverySignVO> list(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", defaultValue = "desc", required = false) String order
            ,String code
            ,String queryFlag
    ) {
        Page<OrderDeliverySignVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("queryFlag",queryFlag);
        params.put("creator", JwtUtils.getLoginUserId());
        Page<OrderDeliverySignVO> list = orderDeliverySignService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "获取详情", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getDetailById")
    public JsonResult<OrderDeliverySignVO> list(
            Long id
    ) {
        Map params=new HashMap();
        params.put("id",id);
        List<OrderDeliverySignVO> list = orderDeliverySignService.list4app(params);
        if (list.size()>0){
            list.get(0).setCustomerName(JwtUtils.getEnterpriseName());
            return JsonResult.success(list.get(0));
        }else{
            return JsonResult.fail("未找到业务数据");
        }

    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long orderDeliverySignId) {
        OrderDeliverySignVO orderDeliverySignVO = orderDeliverySignService.get4appById(orderDeliverySignId);
        if (orderDeliverySignVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), orderDeliverySignVO);
    }

    @PostMapping()
    public JsonResult add(@Validated OrderDeliverySignForm orderDeliverySignForm) {
        if (orderDeliverySignService.add(orderDeliverySignForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @PutMapping()
    public JsonResult update(OrderDeliverySignForm orderDeliverySignForm) {
        OrderDeliverySign orderDeliverySign =
            orderDeliverySignService.getOne(new QueryWrapper<OrderDeliverySign>().eq("id" , orderDeliverySignForm.getId()));
        if (orderDeliverySign ==null){
            return JsonResult.fail(localeMessage.getMessage("common.fail" ));
        }
        if (orderDeliverySignService.updateById(orderDeliverySign)) {
            return JsonResult.success(localeMessage.getMessage("common.success" ));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail" ));
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long orderDeliverySignId) {
        OrderDeliverySign orderDeliverySign =orderDeliverySignService.getById(orderDeliverySignId);
        if (orderDeliverySign ==null){
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        if (orderDeliverySignService.removeById(orderDeliverySignId)) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }


    /**
     * 异常数列表
     * @param id 签收记录id
     */
    @GetMapping("/abnormalList")
    public JsonResult<OrderDeliverySignVO> abnormalList(@RequestParam("id") Long id){
        List<OrderDeliverySignVO> vo = orderDeliverySignService.abnormalList(id);
        return JsonResult.success(vo);
    }
}
